var config = {
	init: function() {
		layui.use(['layer', 'form', 'table', 'admin', 'utilPage'], function() {
			var $ = layui.jquery;
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var admin = layui.admin;
			var utilPage = layui.utilPage;
			/* 渲染表格 */
			var insTb = table.render({
				elem: '#baseTable',
				url: '/sys/config/getPage',
				method: 'post',
				page: true,
				toolbar: '#tableToolBar',
				request: {
					pageName: 'pageNum', //页码的参数名称，默认：page
					limitName: 'pageSize' //每页数据量的参数名，默认：limit
				},
				parseData: function(res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": res.msg, //解析提示文本
						"count": res.page.total, //解析数据长度
						"data": res.page.records //解析数据列表
					};
				},
				cellMinWidth: 100,
				cols: [
					[{
							type: 'checkbox'
						},
						{
							type: 'numbers'
						},
						{
							field: 'description',
							title: '描述'
						},
						{
							field: 'name',
							title: '配置名'
						},
						{
							field: 'val',
							title: '值'
						},
						{
							title: '操作',
							toolbar: '#cellBar',
							align: 'center',
							minWidth: 200
						}
					]
				]
			});

			/* 表格搜索 */
			form.on('submit(tbSearch)', function(data) {
				insTb.reload({
					where: data.field,
					page: {
						curr: 1
					}
				});
				return false;
			});

			/* 表格工具条点击事件 */
			table.on('tool(baseTable)', function(obj) {
				if(obj.data.allowedUpdate == 1){
					layer.msg('该数据不允许修改或删除', {icon: 2});
					return false;
				}
				if (obj.event === 'edit') { // 修改
					showEditModel(obj.data);
				} else if (obj.event === 'del') { // 删除
					doDel(obj);
				}
			});

			/* 表格头工具栏点击事件 */
			table.on('toolbar(baseTable)', function(obj) {
				if (obj.event === 'add') { // 添加
					showEditModel();
				} else if (obj.event === 'del') { // 删除
					var checkRows = table.checkStatus('baseTable');
					if (checkRows.data.length === 0) {
						layer.msg('请选择要删除的数据', {
							icon: 2
						});
						return;
					}
					var ids = checkRows.data.map(function(d) {
						return d.id;
					});
					var allowedUpdates = checkRows.data.map(function(d) {
						return d.allowedUpdate;
					});
					for(var i=0; i<allowedUpdates.length; i++){
						if(allowedUpdates[i] == 1){
							layer.msg('存在不允许修改或删除的数据', {icon: 2});
							return false;
						}
					}
					doDel({
						ids: ids
					});
				}else if (obj.event === 'recycle') { //回收站
					var openWH = utilPage.getOpenWidthHeight();
					var index = admin.open({
						type: 2,
						title: '回收站',
						content: '/sys/config/recycleIndex',
						shade: 0.2,
						maxmin:true,
						shadeClose: true,
						area: [openWH[0] + 'px', openWH[1] + 'px'],
						offset: [openWH[2] + 'px', openWH[3] + 'px'],
						end: function(layero, index){
							insTb.reload();
						}
					});
					layer.full(index);
				}
			});

			/* 显示表单弹窗 */
			function showEditModel(mData) {
				admin.open({
					type: 1,
					title: (mData ? '修改' : '添加') + '配置',
					content: $('#editDialog').html(),
					success: function(layero, dIndex) {
						form.val('userEditForm', mData);
						// 表单提交事件
						form.on('submit(userEditSubmit)', function(data) {
							var loadIndex = layer.load(2);
							$.ajax({
								url: mData ? '/sys/config/update' : '/sys/config/add',
								data: data.field,
								type: 'post',
								dataType: 'json',
								success: function(res) {
									layer.close(loadIndex);
									if (res.code == 200) {
										layer.close(dIndex);
										layer.msg(res.msg, {
											icon: 1
										});
										insTb.reload({
											page: {
												curr: 1
											}
										});

									} else {
										layer.msg(res.msg, {
											icon: 2
										});
									}
								},
								error: function(xhr, status, error) {
									layer.close(loadIndex);
									var res = xhr.responseJSON;
									layer.msg(res.msg, {
										icon: 2,
										anim: 6
									});
								}
							});
							return false;
						});
						// 禁止弹窗出现滚动条
						$(layero).children('.layui-layer-content').css('overflow', 'visible');
					}
				});
			}

			/* 删除 */
			function doDel(obj) {
				layer.confirm('确定要删除选中数据吗？', {
					skin: 'layui-layer-admin',
					shade: .1
				}, function(i) {
					layer.close(i);
					var loadIndex = layer.load(2);
					$.ajax({
						url: '/sys/config/recycle',
						data: {
							ids: obj.ids ? obj.ids.join(',') : (obj.data.id ? obj.data.id : '')
						},
						type: 'post',
						dataType: 'json',
						success: function(res) {
							layer.close(loadIndex);
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1
								});
								insTb.reload({
									page: {
										curr: 1
									}
								});

							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						},
						error: function(xhr, status, error) {
							layer.close(loadIndex);
							var res = xhr.responseJSON;
							layer.msg(res.msg, {
								icon: 2,
								anim: 6
							});
						}
					});
				});
			}
		});
	},
	initRecycle: function() {
		layui.use(['layer', 'form', 'table', 'util', 'admin'], function() {
			var $ = layui.jquery;
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var util = layui.util;
			var admin = layui.admin;
			/* 渲染表格 */
			var insTb = table.render({
				elem: '#baseTable',
				url: '/sys/config/getRecyclePage',
				method: 'post',
				page: true,
				toolbar: '#tableToolBar',
				request: {
					pageName: 'pageNum', //页码的参数名称，默认：page
					limitName: 'pageSize' //每页数据量的参数名，默认：limit
				},
				parseData: function(res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": res.msg, //解析提示文本
						"count": res.page.total, //解析数据长度
						"data": res.page.records //解析数据列表
					};
				},
				cellMinWidth: 100,
				cols: [
					[{
							type: 'checkbox'
						},
						{
							type: 'numbers'
						},
						{
							field: 'description',
							title: '描述'
						},
						{
							field: 'name',
							title: '配置名'
						},
						{
							field: 'val',
							title: '值'
						},
						
						{
							title: '操作',
							toolbar: '#cellBar',
							align: 'center',
							minWidth: 200
						}
					]
				]
			});
		
			/* 表格搜索 */
			form.on('submit(tbSearch)', function(data) {
				insTb.reload({
					where: data.field,
					page: {
						curr: 1
					}
				});
				return false;
			});
	
			/* 表格工具条点击事件 */
			table.on('tool(baseTable)', function(obj) {
				if (obj.event === 'restore') { // 还原
					doRestore(obj);
				} else if (obj.event === 'del') { // 删除
					doDel(obj);
				}
			});
	
			/* 表格头工具栏点击事件 */
			table.on('toolbar(baseTable)', function(obj) {
				if (obj.event === 'restore') { // 还原
					var checkRows = table.checkStatus('baseTable');
					if (checkRows.data.length === 0) {
						layer.msg('请选择要还原的数据', {
							icon: 2
						});
						return;
					}
					var ids = checkRows.data.map(function(d) {
						return d.id;
					});
					doRestore({
						ids: ids
					});
				} else if (obj.event === 'del') { // 删除
					var checkRows = table.checkStatus('baseTable');
					if (checkRows.data.length === 0) {
						layer.msg('请选择要删除的数据', {
							icon: 2
						});
						return;
					}
					var ids = checkRows.data.map(function(d) {
						return d.id;
					});
					doDel({
						ids: ids
					});
				}
			});
	
			/* 还原 */
			function doRestore(obj) {
				layer.confirm('确定要还原选中数据吗？', {
					skin: 'layui-layer-admin',
					shade: .1
				}, function(i) {
					layer.close(i);
					var loadIndex = layer.load(2);
					$.ajax({
						url: '/sys/config/restore',
						data: {
							ids: obj.ids ? obj.ids.join(',') : (obj.data.id ? obj.data.id : '')
						},
						type: 'post',
						dataType: 'json',
						success: function(res) {
							layer.close(loadIndex);
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1
								});
								insTb.reload({
									page: {
										curr: 1
									}
								});
				
							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						},
						error: function(xhr, status, error) {
							layer.close(loadIndex);
							var res = xhr.responseJSON;
							layer.msg(res.msg, {
								icon: 2,
								anim: 6
							});
						}
					});
				});
			}
	
			/* 删除 */
			function doDel(obj) {
				layer.confirm('确定要彻底删除选中数据吗？', {
					skin: 'layui-layer-admin',
					shade: .1
				}, function(i) {
					layer.close(i);
					var loadIndex = layer.load(2);
					$.ajax({
						url: '/sys/config/delete',
						data: {
							ids: obj.ids ? obj.ids.join(',') : (obj.data.id ? obj.data.id : '')
						},
						type: 'post',
						dataType: 'json',
						success: function(res) {
							layer.close(loadIndex);
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1
								});
								insTb.reload({
									page: {
										curr: 1
									}
								});
	
							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						},
						error: function(xhr, status, error) {
							layer.close(loadIndex);
							var res = xhr.responseJSON;
							layer.msg(res.msg, {
								icon: 2,
								anim: 6
							});
						}
					});
				});
			}
	
		});
	}
}
